package cn.pegasus.teamwork.pegasus.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussUserCars;
import cn.pegasus.teamwork.pegasus.mapper.BussUserCarsMapper;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;
import cn.pegasus.teamwork.pegasus.service.BussUserCarsService;
import cn.pegasus.teamwork.util.OSSClientUtil;

@RestController
@RequestMapping("/car")
public class CarsController extends BaseManagerController<BussUserCars,BussUserCarsMapper>{

	 private static final Logger logger = LoggerFactory.getLogger(CarsController.class);
	 
	 @Autowired
	 private BussUserCarsService userCarsService;
	 @Autowired
	 private BussUcenterAreaService ucenterAreaService; 
	 /**
	  * 查询列表
	  */
	 /**
	  * 查询区域信息
	  */
	 @RequestMapping(value = "/list",method = {RequestMethod.GET})
	 public ModelAndView list(@RequestParam Map<String, Object> map){
		 ModelAndView view = new ModelAndView("car/car-list");
		 if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		 try{
			 PageObject<BussUserCars> dicList = userCarsService.PageList(map);
			 view.addObject("list", dicList);
			 view.addObject("map", map);
		 }catch(Exception e){
			 logger.error("查询车辆列表信息异常",e.getMessage());
		 }
		 return view;
	 }
	 
	 /**
	  * 跳转修改
	  */
	 @RequestMapping(value="/goAdd" ,method = {RequestMethod.GET})
	 public ModelAndView goAdd(
			 @RequestParam(value="id",required = false) Long id) throws Exception{
		 ModelAndView view = new ModelAndView("car/car-edit");
		// List<BussUcenterArea> areaList = ucenterAreaService.findList(null, 4);
		 BussUserCars area = userCarsService.findCarInfo(id);
		 view.addObject("info", area);
		// view.addObject("list", areaList);
		 return view;
	 }
	 /**
	  * 修改
	  */
	 @ResponseBody
	 @RequestMapping(value="/updateCar", method = RequestMethod.POST)
	 public String updateCar(HttpServletRequest request,
			 @RequestParam(value = "carCode", required = false) String carCode,
			 @RequestParam(value = "carBrand", required = false) String carBrand,
			 @RequestParam(value = "carModel", required = false) String carModel,
			 @RequestParam(value = "carColor", required = false) String carColor,
			 @RequestParam(value = "linkMan", required = false) String linkMan,
			 @RequestParam(value = "linkTel", required = false) String linkTel,
			 @RequestParam(value = "areaAddress", required = false) String areaAddress,
			 @RequestParam(value = "areaId", required = false) Integer areaId,
			 @RequestParam(value = "id", required = false) Long id){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	BussUserCars info = new BussUserCars();
        	info.setCarCode(carCode);
        	info.setCarBrand(carBrand);
        	info.setCarModel(carModel);
        	info.setLinkMan(linkMan);
        	info.setLinkTel(linkTel);
        	info.setCarColor(carColor);
        	info.setAreaAddress(areaAddress);
        	info.setAreaId(areaId);
        	info.setId(id);
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String carImages = util.uploadImg2Oss(file);
            	info.setCarImages(carImages);
        	}
        	userCarsService.updateCar(getAdmin().getId(), info);
        } catch (Exception e) {
            logger.error("修改车辆信息异常", e);
            return "1001";
        }
        return "200";
	 }
}
